var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    radioItems: [],
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
        // 判断是否登录
    app.getUserInfoFun();
    var u_id = wx.getStorageSync('u_id');
    this.setData({
      u_id: u_id,
    })

    //获取车辆列表
    var url = "api/car/carsFind";
    var param = {
      u_id: this.data.u_id,
    }
    app.getRequest(url, param, res => {
      if (res.code == 202) {
        wx.showModal({
          title: '暂无车辆，请添加车辆',
          icon: 'none',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../carManage_add/carManage_add'
              })
            } else if (res.cancel) {
              wx.switchTab({
                url: '../index/index'
              })
            }
          }
        })
        // setTimeout(function () {
        //   wx.navigateTo({
        //     url: '../carManage_add/carManage_add'
        //   })
        // },1000);
      } else if (res.code == 200) {
        var len = res.data.length;
        var item = res.data;
        for (var i = 0; i < len; i++) {
          item[i].logo = app.globalData.globalUrl + item[i].logo;
          if (item[i].logo == app.globalData.globalUrl) {
            item[i].logo = '../../images/carManage/images/che_logo.png'
          }
          item[i].value = i;
          if (item[i].defaultcar == 2) {
            item[i].checked = true;
            wx.setStorageSync('c_id', item[i].c_id);
          }
        }
        this.setData({
          radioItems: res.data,
        })

      }
    });
  },

  //选择默认车辆
  radioChange: function (e) {
    //单选点击事件
    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == parseInt(e.detail.value);
      if (radioItems[i].checked) {
        this.setData({
          c_id: radioItems[i].c_id,
        });
        wx.setStorageSync('c_id', radioItems[i].c_id);
      }
    }

    this.setData({
      radioItems: radioItems,
    });

    var url = "api/car/carDefault";
    var param = {
      u_id: this.data.u_id,
      c_id: this.data.c_id,
    }
    app.getRequest(url, param, res => {
      if (res.code == 200) {
        wx.showToast({
          title: '已选中此车辆',
          type: 'success',
          duration: 2000,
        })
      }
    });
  },

  //点击车辆跳转到编辑车辆页面
  navigatorTo: function (e) {
    var c_id = e.currentTarget.dataset.cid;
    wx.navigateTo({
      url: '../carManage_edit/carManage_edit?c_id=' + c_id,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});